<?php

class Application_Model_Item extends App_Model
{
    protected $_id;
    protected $_reference;
    protected $_price;
    protected $_itemOrders = array();
    protected $_itemCreditNotes = array();

    public function getId() {
        return $this->_id;
    }

    public function setInvoice($invoice) {
        $this->_invoice = $invoice;
        return $this;
    }

        public function setId($id) {
        $this->_id = $id;
        return $this;
    }

    public function getReference() {
        return $this->_reference;
    }

    public function setReference($reference) {
        $this->_reference = $reference;
        return $this;
    }

    public function getPrice() {
        return $this->_price;
    }

    public function setPrice($price) {
        $this->_price = $price;
        return $this;
    }

    public function setItemOrders(array $itemOrders){
        $this->_itemOrders = $itemOrders;
        return $this;
    }

    public function getItemsOrders(){
        return $this->_itemOrders;
    }

    public function getItemCreditNotes() {
        return $this->_itemCreditNotes;
    }

    public function setItemCreditNotes($itemCreditNotes) {
        $this->_itemCreditNotes = $itemCreditNotes;
        return $this;
    }


    /*
     *  Ajoute une ligne de commande à l'objet Item
     * @param $itemOrder Application_Model_ItemOrder
     */
    public function addItemOrder(Application_Model_ItemOrder $itemOrder)
    {
        $this->_itemOrders[] = $itemOrder;
    }

    /*
     *  Ajoute une ligne d'avoir à l'objet Item
     *  @param $itemCreditNote Application_Model_ItemCreditNote
     */
    public function addItemCreditNote($itemCreditNote)
    {
        $this->_itemCreditNotes[] = $itemCreditNote;
    }

    /*
     *  Calcul le chiffre d'affaire de l'article pour une tranche donnée
     *  @param $begin int
     *  @param $end int
     *  @return $total float
     */
    public function calculateTurnover($begin, $end)
    {
        $total = 0;        
        foreach($this->_itemOrders as $itemOrder){
            //echo 'date envoi ' . $itemOrder->getOrder()->getCustomerOrder()->getSendAt() . 'entre ' . $begin . ' et ' . $end . '<br/>';
            if($itemOrder->getOrder()->getCustomerOrder()->getSentAt() >= $begin
                    && $itemOrder->getOrder()->getCustomerOrder()->getSentAt() <= $end
                    && 0 == $itemOrder->getOrder()->getCustomerOrder()->getRefused()
                    && '' != $itemOrder->getOrder()->getCustomerOrder()->getInvoice()){
                $total += $itemOrder->getPrice() * $itemOrder->getQuantity();
            }
        }
        
        foreach($this->_itemCreditNotes as $itemCreditNote){
            if($itemCreditNote->getCreditNote()->getCreatedAt() >= $begin
                    && $itemCreditNote->getCreditNote()->getCreatedAt() <= $end){
                $total = $total - $itemCreditNote->getTotal();
            }
        }

        return $total;
    }
}

